package com.cloud.client.controller;

import com.cloud.client.service.JobService;
import com.cloud.client.util.AjaxResult;
import com.cloud.client.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @packageName: com.cloud.client.controller
 * @name: JobController
 * @description:
 * @author: 董定卓
 * @dateTime: 2019/7/17 10:24
 */
@RestController
public class JobController {
    @Autowired
    JobService service;

    /**
     * 添加任务
     *
     * @param map
     * @return
     */
    @PostMapping("/jobAdd")
    public Object jobAdd(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return AjaxResult.failResult("任务名称不能为空");
        }
        String groupName = MapUtil.getString(map, "groupName");
        if (groupName == null) {
            return AjaxResult.failResult("任务组名称不能为空");
        }
        String cronExp = MapUtil.getString(map, "cronExp");
        if (cronExp == null) {
            return AjaxResult.failResult("cron表达式不能为空");
        }
        return service.jobAdd(map);
    }

    /**
     * 暂停任务
     *
     * @param map
     * @return
     */
    @PostMapping("/jobPause")
    public Object jobPause(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return AjaxResult.failResult("任务名称不能为空");
        }
        String groupName = MapUtil.getString(map, "groupName");
        if (groupName == null) {
            return AjaxResult.failResult("任务组名称不能为空");
        }
        return service.jobPause(map);
    }

    /**
     * 恢复任务
     *
     * @param map
     * @return
     */
    @PostMapping("/jobResume")
    public Object jobResume(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return AjaxResult.failResult("任务名称不能为空");
        }
        String groupName = MapUtil.getString(map, "groupName");
        if (groupName == null) {
            return AjaxResult.failResult("任务组名称不能为空");
        }
        return service.jobResume(map);
    }

    /**
     * 更新任务
     *
     * @param map
     * @return
     */
    @PostMapping("/jobUpdate")
    public Object jobUpdate(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return AjaxResult.failResult("任务名称不能为空");
        }
        String groupName = MapUtil.getString(map, "groupName");
        if (groupName == null) {
            return AjaxResult.failResult("任务组名称不能为空");
        }
        String cronExp = MapUtil.getString(map, "cronExp");
        if (cronExp == null) {
            return AjaxResult.failResult("cron表达式不能为空");
        }
        return service.jobUpdate(map);
    }

    /**
     * 删除任务
     *
     * @param map
     * @return
     */
    @PostMapping("/jobDel")
    public Object jobDel(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return AjaxResult.failResult("任务名称不能为空");
        }
        String groupName = MapUtil.getString(map, "groupName");
        if (groupName == null) {
            return AjaxResult.failResult("任务组名称不能为空");
        }
        return service.jobDel(map);
    }

    /**
     * 开启所有任务
     *
     * @param map
     * @return
     */
    @PostMapping("/jobStartAll")
    public Object jobStartAll(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        return service.jobStartAll(map);
    }

    /**
     * 停止所有任务
     *
     * @param map
     * @return
     */
    @PostMapping("/jobShutdownAll")
    public Object jobShutdownAll(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        return service.jobShutdownAll(map);
    }


    /**
     * 任务分页列表
     *
     * @param map
     * @return
     */
    @PostMapping("/jobPage")
    public Object jobPage(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        return service.jobPage(map);
    }


}
